package com.rui.study.test.ehcache.controller;

import com.rui.study.test.ehcache.entity.User;
import com.rui.study.test.ehcache.service.UserService;
import com.rui.study.test.ehcache.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @program: study
 * @description:
 * @author: Yaowr
 * @create: 2019-03-07 14:36
 **/
@RestController
public class UserController {
    public static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @PutMapping("/user")
    public ResponseEntity createUser(@RequestBody User user) {
        LOGGER.debug("创建用户: {}", JsonUtil.toJson(user));
        userService.createUser(user);
        return ResponseEntity.ok().build();
    }
    @GetMapping("/user")
    public ResponseEntity getUser(@RequestParam("union_id") int unionId) {
        LOGGER.debug("查询用户：{}", unionId);
        return ResponseEntity.ok(userService.getUser(unionId));
    }
    @PostMapping("/user")
    public ResponseEntity modifyUser(@RequestBody User user) {
        LOGGER.debug("修改用户：{}", JsonUtil.toJson(user));
        userService.modifyUser(user);
        return ResponseEntity.ok().build();
    }
    @DeleteMapping("/user")
    public ResponseEntity deleteUser(@RequestParam("union_id") int unionId) {
        LOGGER.debug("删除用户：{}", unionId);
        userService.deleteUser(unionId);
        return ResponseEntity.ok().build();
    }
}
